package com.danceforyou.common.service.impl;

import com.danceforyou.common.model.BaseDO;
import com.danceforyou.common.model.view.BizData4Page;
import com.danceforyou.common.service.BaseService;
import com.danceforyou.common.dao.BaseDAO;
import com.danceforyou.common.vo.SqlOrderEnum;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 类名称：BaseServiceImpl
 * 创建人：danceforyou
 * 创建时间：2020年10月23日
 * 版本号：1.0v
 */
public abstract class BaseServiceImpl<T, DO extends BaseDO<T>, VO extends DO, DAO extends BaseDAO<T, DO, VO>> implements BaseService<T, DO, VO, DAO> {

    @Override
    public int insert(DO entity) {
        return (this.getDao()).insert(entity);
    }

    @Override
    public int update(DO entity) {
        return this.getDao().update(entity);
    }

    @Override
    public int updateWhole(DO entity) {
        return this.getDao().updateWhole(entity);
    }

    @Override
    public int updateByProperty(DO entity, String property, Object value) {
        return this.getDao().updateByProperty(entity, property, value);
    }

    @Override
    public int updateWholeByProperty(DO entity, String property, Object value) {
        return this.getDao().updateWholeByProperty(entity, property, value);
    }

    @Override
    public int delete(DO entity) {
        return this.getDao().deleteById(entity.getId());
    }

    @Override
    public int deleteById(Object id) {
        return this.getDao().deleteById(id);
    }

    @Override
    public int deleteByIds(List<T> idList) {
        return this.getDao().deleteByIds(idList);
    }

    @Override
    public int deleteByProperty(String property, Object value) {
        return this.getDao().deleteByProperty(property, value);
    }

    @Override
    public int deleteByCondition(Map<String, Object> condition) {
        return this.getDao().deleteByCondition(condition);
    }

    @Override
    public int setDelete(DO entity) {
        return this.getDao().setDeleteById(entity.getId());
    }

    @Override
    public int setDeleteById(T id) {
        return this.getDao().setDeleteById(id);
    }

    @Override
    public int setDeleteByIds(List<T> idList) {
        return this.getDao().setDeleteByIds(idList);
    }

    @Override
    public int setDeleteByProperty(String property, Object value) {
        return this.getDao().setDeleteByProperty(property, value);
    }

    @Override
    public int setDeleteByCondition(Map<String, Object> condition) {
        return this.getDao().setDeleteByCondition(condition);
    }

    @Override
    public DO getById(T id) {
        return this.getDao().getById(id);
    }

    @Override
    public DO getByProperty(String property, Object value) {
        return this.getDao().getByProperty(property, value, null, null);
    }

    @Override
    public DO getByProperty(String property, Object value, String var3, SqlOrderEnum var4) {
        return this.getDao().getByProperty(property, value, var3, var4.getAction());
    }

    @Override
    public DO getByCondition(Map<String, Object> condition) {
        return this.getDao().getByCondition(condition, null, null);
    }

    @Override
    public DO getByCondition(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().getByCondition(condition, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public List<DO> listAll() {
        return this.getDao().listAll(null, null);
    }

    @Override
    public List<DO> listAll(String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().listAll(orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public List<DO> listByProperty(String property, Object value) {
        return this.getDao().listByProperty(property, value, null, null);
    }

    @Override
    public List<DO> listByProperty(String property, Object value, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().listByProperty(property, value, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public List<DO> listByCondition(Map<String, Object> condition) {
        return this.getDao().listByCondition(condition, null, null);
    }

    @Override
    public List<DO> listByCondition(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().listByCondition(condition, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public List<VO> queryList(Map<String, Object> condition) {
        return this.getDao().queryList(condition, null, null);
    }

    @Override
    public List<VO> queryList(Map<String, Object> condition, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().queryList(condition, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public List<VO> queryPage(Map<String, Object> condition, int offset, int rows, String orderBy, SqlOrderEnum sqlOrderEnum) {
        return this.getDao().queryPage(condition, offset, rows, orderBy, sqlOrderEnum.getAction());
    }

    @Override
    public Integer countByCondition(Map<String, Object> condition) {
        return this.getDao().countByCondition(condition);
    }

    @Override
    public Map<String, Object> statsByCondition(Map<String, Object> condition) {
        return this.getDao().statsByCondition(condition);
    }

    @Override
    public BigDecimal sumByCondition(String property, Map<String, Object> condition) {
        return this.getDao().sumByCondition(property, condition);
    }

    @Override
    public void saveOrUpdate(DO entity) {
        if(entity.getId() != null) {
            this.getDao().update(entity);
        } else {
            this.getDao().insert(entity);
        }

    }

    @Override
    public void queryPageByDataPerm(BizData4Page<VO> bizData4Page) {
        if (bizData4Page.getPageNo() <= 0) {
            bizData4Page.setPageNo(1);
        }
        int offset = (bizData4Page.getPageNo() - 1) * bizData4Page.getPageSize();
        int pageSize = bizData4Page.getPageSize();
        //进行分页查询条目
        bizData4Page.setRowList(this.getDao().queryPage(bizData4Page.getCondition(), offset, pageSize, bizData4Page.getOrderBy(), bizData4Page.getSortBy()));
        //查询当前条件数据总条数
        bizData4Page.setTotal(this.getDao().countByCondition(bizData4Page.getCondition()));
        bizData4Page.setPage(bizData4Page.getPage());
        if (bizData4Page.isStat()) {
            bizData4Page.setStatData(this.getDao().statsByCondition(bizData4Page.getCondition()));
        }
        int page = pageSize == 0 ? 0 : bizData4Page.getTotal() / pageSize;
        int mod = pageSize == 0 ? 0 : bizData4Page.getTotal() % pageSize;
        if (mod > 0) {
            page ++;
        }
        bizData4Page.setPage(page);
    }
}
